package day12.HomeWork;

import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//a.参考课上讲述的定长线程池的案例 通过可缓存线程池 去完成下列需求
//现在餐厅生意越来越好了，老板直接跟大厨说，你能炒多快，就炒多快，灶台随时可以加，好好干，工资不会少。
// 同样用线程池模拟炒五个菜的过程，并且统计炒完共计花费了多久
public class Demo2 {
    public static void main(String[] args) {
        String[] names = {"辣椒炒肉", "西红柿炒鸡蛋", "红烧排骨", "爆炒鱿鱼", "绝味蜜汁鸡"};
        long[] tmies = {1000, 2000, 3000, 4000, 5000};
        ExecutorService es = Executors.newCachedThreadPool();
        long start = System.currentTimeMillis();
        for (int i = 0; i < names.length; i++) {
            int index = i;
            es.execute(() -> {
                try {
                    System.out.println("开始炒" + names[index]);
                    Thread.sleep(tmies[index]);
                    System.out.println("已经炒好了" + names[index] + "可以上菜了");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        es.shutdown();
        while (true) {
            if (es.isTerminated()) {
                long end = System.currentTimeMillis();
                System.out.println("全部菜抄完了,共花了" + (end - start) + "ms");
                break;
            }
        }
    }
}
